import os
import ctypes
import sys
import requests


def get_hosts(hosts='C:\\Windows\\System32\\drivers\\etc\\hosts'):
    url = "https://raw.staticdn.net/googlehosts/hosts/master/hosts-files/hosts"
    header = {
        'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
        ' Chrome/83.0.4103.61 Safari/537.36 Edg/83.0.478.37'
    }
    try:
        hosts_w = requests.get(url=url, headers=header).text
        with open(hosts, "w") as f:
            f.write(hosts_w)
        return False
    except Exception:
        return True


def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except Exception:
        return False


# 以管理员身份运行
if is_admin():
    while get_hosts():
        pass
    os.popen("ipconfig /flushdns")
else:
    if sys.version_info[0] == 3:
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable,
                                            __file__, None, 1)
